package Service.Teacher.Imp;

import Dao.Role.AdminDao;
import Dao.DaoFactory;
import Dao.Role.StuInfoDao;
import Dao.Role.TeacherDao;
import Entity.Role.Admin;
import Entity.Role.Student;
import Entity.Role.Teacher;
import Service.Teacher.LoginService;
import Util.DBUtil;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class LoginServiceImp implements LoginService {

    private final StuInfoDao stuInfoDao;
    private final TeacherDao teacherDao;
    private final AdminDao adminDao;
    private final Connection conn;
    static ArrayList<String> userLogin = new ArrayList<>(); //用于存放已经登录的用户的账户

    public LoginServiceImp() {
        stuInfoDao = DaoFactory.getInstance("stuInfoDao");
        teacherDao = DaoFactory.getInstance("teacherDao");
        adminDao = DaoFactory.getInstance("adminDao");
        conn = DBUtil.getConnByDruid();
    }

    /**
     * 学生登录
     *
     * @param student 要登陆的学生对象
     * @return 1001 登录成功 ，1002 密码错误 ，1003 账号不存在 . 1004 登录失败  1005 用户已经登录
     */
    @Override
    public Integer stuLogin(Student student) {
        int code = 1004;
        //判断是否已经登录
        int userLoginSum = Collections.frequency(userLogin, student.getSNO());
        if (userLoginSum == 0) {
            List<Student> students = stuInfoDao.getStuByNO(student.getSNO(), conn);
            if (students.size() == 1) {
                if (students.get(0).getPassword().equals(student.getPassword())) {
                    code = 1001;
                    userLogin.add(student.getSNO());
                } else {
                    code = 1002;
                }
            } else if (students.size() == 0) {
                code = 1003;
            }
            DBUtil.closeConn(conn);
        } else {
            code = 1005;
        }
        DBUtil.closeConn(conn);
        return code;
    }

    /**
     * 老师登录
     *
     * @param teacher 要登陆的老师对象
     * @return 1001 登录成功 ，1002 密码错误 ，1003 账号不存在 . 1004 登录失败
     */
    @Override
    public Integer teacherLogin(Teacher teacher) {
        int code = 1004;
        int userLoginSum = Collections.frequency(userLogin, teacher.getTNO());
        if (userLoginSum == 0) {
            List<Teacher> teachers = teacherDao.getTeacherByNO(teacher.getTNO(), conn);
            if (teachers.size() == 0) {
                code = 1003;
            } else if (teachers.size() == 1) {
                if (teachers.get(0).getPassword().equals(teacher.getPassword())) {
                    code = 1001;
                    userLogin.add(teacher.getTNO());
                } else {
                    code = 1002;
                }
            }
        } else {
            code = 1005;
        }
        DBUtil.closeConn(conn);
        return code;
    }

    @Override
    public Integer adminLogin(Admin admin) {
        int code = 1004;
        int userLoginSum = Collections.frequency(userLogin, admin.getAdminName());
        if (userLoginSum == 0) {
            List<Admin> admins = adminDao.getAdminByName(admin.getAdminName(), conn);
            if (admins.size() == 1) {
                if (admins.get(0).getAdminPassword().equals(admin.getAdminPassword())) {
                    code = 1001;
                    userLogin.add(admin.getAdminName());
                } else {
                    code = 1002;
                }
            } else if (admins.size() == 0) {
                code = 1003;
            }
        } else {
            code = 1005;
        }
        DBUtil.closeConn(conn);
        return code;
    }

    @Override
    public Object getInfoByNOAndRole(String NO, String role) {
        Object obj = null;
        if ("1".equals(role)) {
            List<Student> students = stuInfoDao.getStuByNO(NO, conn);
            if (students.size() == 1) {
                students.get(0).setPassword(null);
                obj = students.get(0);
            }
        } else if ("2".equals(role)) {
            List<Teacher> teachers = teacherDao.getTeacherByNO(NO, conn);
            if (teachers.size() == 1) {
                teachers.get(0).setPassword(null);
                obj = teachers.get(0);
            }
        } else if ("3".equals(role)) {
            List<Admin> admins = adminDao.getAdminByName(NO, conn);
            if (admins.size() == 1) {
                admins.get(0).setAdminPassword(null);
                obj = admins.get(0);
            }
        }
        DBUtil.closeConn(conn);
        return obj;
    }

    @Override
    public Integer updatePassByNOAndRole(String NO, String role, String oldPass, String newPass) {
        int code = 1003;
        if ("1".equals(role)) {
            List<Student> students = stuInfoDao.getStuByNO(NO, conn);
            if (students.size() == 1) {
                if (oldPass.equals(students.get(0).getPassword())) {
                    if (stuInfoDao.updatePassBySNO(NO, newPass, conn) == 1) {
                        code = 1001;
                    }
                } else {
                    code = 1002;
                }
            }
        } else if ("2".equals(role)) {
            List<Teacher> teachers = teacherDao.getTeacherByNO(NO, conn);
            if (teachers.size() == 1) {
                if (oldPass.equals(teachers.get(0).getPassword())) {
                    if (teacherDao.updatePassByTNO(NO, newPass, conn) == 1) {
                        code = 1001;
                    }
                } else {
                    code = 1002;
                }
            }
        } else if ("3".equals(role)) {
            List<Admin> admins = adminDao.getAdminByName(NO, conn);
            if (admins.size() == 1) {
                if (oldPass.equals(admins.get(0).getAdminPassword())) {
                    if (adminDao.updatePassByNO(NO, newPass, conn) == 1) {
                        code = 1001;
                    }
                } else {
                    code = 1002;
                }
            }
        }
        DBUtil.closeConn(conn);
        return code;
    }

    @Override
    public void removeUserLogin(String NO) {
        userLogin.remove(NO);
    }
}
